package com.luxoft.scoremanager.server.dao.hibernateimpl;

import com.luxoft.scoremanager.server.dao.ComponentDao;
import com.luxoft.scoremanager.server.dao.exception.HibernateRecordException;
import com.luxoft.scoremanager.server.model.entity.Component;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class HibernateComponentDao implements ComponentDao {

    private static Logger LOG = Logger.getLogger(HibernateComponentDao.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addComponent(Component component) {
        Session session = null;
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.saveOrUpdate(component);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOG.error("Error component add");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void updateComponent(Component component) {
        Session session = null;
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.update(component);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOG.error("Error component update");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
    }

    @Override
    public void deleteComponent(Component component) {
        Session session = null;
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.delete(component);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOG.error("Error component delete");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
    }

    @Override
    public List<Component> getAll() {
        Session session = null;
        List componentList = new ArrayList<Component>();
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            componentList = session.createCriteria(Component.class).list();
        } catch (Exception e) {
            LOG.error("Error component get all");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return componentList;
    }

    @Override
    public Component getById(int id) {
        Session session = null;
        Component component = new Component();
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            component = (Component) session.get(Component.class, id);
        } catch (Exception e) {
            LOG.error("Error component get by id");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return component;
    }
}
